package com.arges.web.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

public class FTPHandle
{
	public static boolean upload(String url, int port, String username, String password, String path, String filename,
			InputStream input)
	{
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try
		{
			int reply;
			ftp.connect(url, port);// 连接FTP服务器
			// 如果采用默认端口，可以使用ftp.connect(url)的方式直接连接FTP服务器
			ftp.login(username, password);
			ftp.setControlEncoding("GBK");
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply))
			{
				ftp.disconnect();
				return result;
			}
			// 进入文件夹
			boolean flag = ftp.changeWorkingDirectory(path);
			if (flag)
			{
				result = ftp.storeFile(new String(filename.getBytes("GBK"), "iso-8859-1"), input);
			}
			input.close();
			ftp.logout();
		}
		catch (SocketException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (ftp.isConnected())
			{
				try
				{
					ftp.disconnect();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean downFile(String url, int port, String username, String password, String remotePath,
			String fileName, String localPath)
	{
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try
		{
			int reply;
			ftp.connect(url, port);
			ftp.login(username, password);
			ftp.setControlEncoding("GBK");
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply))
			{
				ftp.disconnect();
				return result;
			}
			ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
			FTPFile[] fs = ftp.listFiles();
			for (FTPFile ff : fs)
			{
				if (ff.getName().equals(fileName))
				{
					File localFile = new File(localPath + "/" + ff.getName());

					OutputStream is = new FileOutputStream(localFile);
					ftp.retrieveFile(ff.getName(), is);
					is.close();
				}
			}

			ftp.logout();
			result = true;
		}
		catch (SocketException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (ftp.isConnected())
			{
				try
				{
					ftp.disconnect();
				}
				catch (IOException ioe)
				{
				}
			}
		}

		return result;
	}

	public static void main(String[] arges)
	{
		// 上传测试用例
		// try
		// {
		// FileInputStream in = new FileInputStream(new File("D:\\demo测试.txt"));
		// String url = "192.168.3.124";
		// int port = 21;
		// String username = "zhaoxinjie";
		// String password = "zhao31415926";
		// String path = "/upload";
		// String filename = "test测试.txt";
		// boolean flag = upload(url, port, username, password, path, filename, in);
		// System.out.println("flag = " + flag);
		// }
		// catch (FileNotFoundException e)
		// {
		// e.printStackTrace();
		// }

		// 下载测试用例
		String url = "192.168.3.124";
		int port = 21;
		String username = "zhaoxinjie";
		String password = "zhao31415926";
		String remotePath = "/upload";
		String localPath = "D:/test";
		String filename = "test测试.txt";
		boolean flag = downFile(url, port, username, password, remotePath, filename, localPath);
		System.out.println("flag = " + flag);

	}

}
